
import java.rmi.*;
import java.rmi.registry.*;
import java.util.*;
import org.dea.mapping.*;

public class SwitchSum
{    Map distributedComponentsList = null;

    public SwitchSum() throws RemoteException, Exception {

        	distributedComponentsList = EasyAcceptMappingReader.processMappingFile( "test/xml/mappingSum.xml");

    }

    public SwitchSum(String[] args) throws RemoteException, Exception {

        	distributedComponentsList = EasyAcceptMappingReader.processMappingFile( "test/xml/mappingSum.xml");

    }

    public String sum(int v1, int v2, String switchID) throws RemoteException, RemoteException {
        return getFacade(switchID).sum(v1,v2);
    }

    private IFSum getFacade(String switchID) {
        DistributedComponent cpm = (DistributedComponent) distributedComponentsList.get( switchID );
        if( cpm == null ) 		throw new RuntimeException("Unmapped switchID: "+switchID);
        String hostAddress = cpm.getHostAddress() + ":" + cpm.getHostPort();
        IFSum obj = null;
        try{
        Registry reg = LocateRegistry.getRegistry(cpm.getHostAddress(), cpm.getHostPort());
        	obj = (IFSum) reg.lookup("IFSum");
        } catch (Exception ex){
        	ex.printStackTrace();
        }

        return obj;

    }

} // end SwitchSum
